package com.briup.day20;

import java.util.*;

/**
 * @author ThinkBook
 */
public class Test05_Game {
    //1.定义Map<数值，权重值>
    private  static Map<String,Integer> map;

    static {
        map = new HashMap<>();
        map.put("3", 1);
        map.put("4", 2);
        map.put("5", 3);
        map.put("6", 4);
        map.put("7", 5);
        map.put("8", 6);
        map.put("9", 7);
        map.put("10", 8);
        map.put("J", 9);
        map.put("Q", 10);
        map.put("K", 11);
        map.put("A", 12);
        map.put("2", 13);
        map.put("小王", 14);
        map.put("大王", 15);
    }
    private static int getWeight(String number){
        return map.get(number);
    }
    public static void main(String[] args) {
//1.创建54张牌
        //(1).准备花色
        List<String> color = new ArrayList<>();
        Collections.addAll(color,"♠", "♥", "♣", "♦");
        //(2).准备数字
        List<String> number = new ArrayList<>();
        Collections.addAll(number,"A", "2", "3", "4", "5", "6",
                "7", "8", "9", "10", "J", "Q", "K");
        //(3).准备卡片集合
        List<Card> cards = new ArrayList<>();
        //(4).根据花色和数字组成54张牌,并放入集合
        for (int i =0;i<color.size();i++){

            for (int j = 0;j<number.size();j++){
                String color1 =color.get(i);
                String number1 =number.get(j);
                int weight = Test05_Game.getWeight(number1);
                Card card = new Card(color1,number1,weight);
                cards.add(card);
            }
        }
       cards.add(new Card("","大王",Test05_Game.getWeight("大王")));
       cards.add(new Card("","小王",Test05_Game.getWeight("小王")));
//2.打乱
        Collections.shuffle(cards);
//3.发牌
      //(1).创建三个玩家和底牌集合
      List<Card> player1 = new ArrayList<>();
      List<Card> player2 = new ArrayList<>();
      List<Card> player3 = new ArrayList<>();
      List<Card> dipai = new ArrayList<>();
      //(2).放入
        for (int i = 0; i < cards.size(); i++){
            Card c= cards.get(i);
            if (i>=51){
                dipai.add(c);
            }else if (i % 3 ==0){
                player1.add(c);
            }else if(i % 3 ==1){
                player2.add(c);
            }else if(i % 3 ==2){
                player3.add(c);
            }
        }
//4.看牌
        //(1).排序,根据权重比排序升序
        Comparator<Card> comp = new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                int r = o1.getWeight()- o2.getWeight();
                return r;
            }
        };
        //(2).对每一个玩家和底牌进行排序
        Collections.sort(player1,comp);
        Collections.sort(player2,comp);
        Collections.sort(player3,comp);
        Collections.sort(dipai,comp);
        //(3).用map集合存放玩家对应的牌
        Map<String,List<Card>> map = new HashMap<>();
        map.put("player1",player1);
        map.put("player2",player2);
        map.put("player3",player3);
        map.put("dipai",dipai);
        //(4).遍历查看
        for (String key : map.keySet()){
            System.out.println(key + ": " + map.get(key));
        }
    }
}
class Card{
    // 花色
    String color;
    // 点数
    String number;
    // 权重（比较大小用的）
    int weight;

    public Card(String color, String number, int weight) {
        this.color = color;
        this.number = number;
        this.weight = weight;
    }

    public String getColor() {
        return color;
    }

    public String getNumber() {
        return number;
    }

    public int getWeight() {
        return weight;
    }

    @Override
    public String toString() {
        return color+number;
    }
}
